using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Apache.Cassandra;


namespace Cassandraemon
{


	public static class ColumnExtensions
	{
		public static Column SetName(this Column source, object name)
		{
			source.Name = name.ToCassandraByte();
			return source;
		}
		
		public static Column SetValue(this Column source, object value)
		{
			source.Value = value.ToCassandraByte();
			return source;
		}
		
		public static Column SetTimeStamp(this Column source, long unixTime)
		{
			source.Timestamp = unixTime;
			return source;
		}
		
		public static Column SetTtl(this Column source, int ttl)
		{
			source.Ttl = ttl;
			return source;
		}
		
		
		public static Column SetNameValue(this Column source, object name, object value)
		{
			source.Name = name.ToCassandraByte();
			source.Value = value.ToCassandraByte();
			return source;
		}
		
		public static Column SetNameValueTimestamp(this Column source, object name, object value, long unixTime)
		{
			source.SetNameValue(name, value);
			source.Timestamp = unixTime;
			return source;
		}
		
		public static Column SetNameValueTtl(this Column source, object name, object value, int ttl)
		{
			source.SetNameValue(name, value);
			source.Ttl = ttl;
			return source;
		}
		
		public static Column SetNameValueTimestampTtl(this Column source, object name, object value, long unixTime, int ttl)
		{
			source.SetNameValue(name, value);
			source.Timestamp = unixTime;
			source.Ttl = ttl;
			return source;
		}
		
		public static List<Column> Add(this ICollection<Column> source, object name, object value)
		{
			source.Add(new Column().SetNameValue(name, value));
			return source.ToList();
		}
		
		public static List<Column> Add(this ICollection<Column> source, object name, object value, long unixTime)
		{
			source.Add(new Column().SetNameValueTimestamp(name, value, unixTime));
			return source.ToList();
		}
		
		public static List<Column> Add(this ICollection<Column> source, object name, object value, int ttl)
		{
			source.Add(new Column().SetNameValueTtl(name, value, ttl));
			return source.ToList();
		}
		
		public static List<Column> Add(this ICollection<Column> source, object name, object value, long unixTime, int ttl)
		{
			source.Add(new Column().SetNameValueTimestampTtl(name, value, unixTime, ttl));
			return source.ToList();
		}
		
		public static List<Column> SetTtl(this ICollection<Column> source, int ttl)
		{
			foreach(var column in source)
			{
				column.Ttl = ttl;
			}
			return source.ToList();
		}

		public static byte[] NameOrDefault(this ColumnOrSuperColumn column)
		{
			var col = column.Column;
			if (col != null)
			{
				return col.Name;
			}
			var sco = column.Super_column;
			if (sco != null)
			{
				return sco.Name;
			}
			var ccol = column.Counter_column;
			if (ccol != null)
			{
				return ccol.Name;
			}
			var csco = column.Counter_super_column;
			if (csco != null)
			{
				return csco.Name;
			}
			return null;
		}

		#region Comparisons

		static readonly IEqualityComparer<Column> _ColumnComparerByName =
			new SelectComparer<Column, byte[]>(
				column => column.Name,
				SequenceComparer<byte>.Default);

		internal static IEqualityComparer<Column> ColumnComparerByName
		{
			get { return ColumnExtensions._ColumnComparerByName; }
		}

		#endregion
	}
}
